<?php
namespace app\controller\Eldata;
use Elasticsearch\ClientBuilder;

class Eldata
{

    protected $client=null;

    protected $host=[
        '192.168.17.128:9200'
    ];

    public function __construct()
    {
        if($this->client === null){
            $this->client=ClientBuilder::create()
            ->setHosts($this->hosts)
            ->build();
        }

        if($this->client==null){
            return false;
        }
    }

   /*[
       'view_data' => [
           '_source' => [
               'enabled' => true,
           ],
           'properties' => [
               'view_type' => [
                   'type' => 'integer',
                   'analyzer' => 'standard'
               ],
               'view_search_url' => [
                   'type' => 'string',
                   'analyzer' => 'no_analyzed'
               ],
               'view_copy_data' => [
                   'type' => 'string',
                   'analyzer' => 'no_analyzed'
               ],
               'view_cur_url' => [
                   'type' => 'string',
                   'analyzer' => 'no_analyzed'
               ],
               'view_platform' => [
                   'type' => 'string',
                   'analyzer' => 'no_analyzed'
               ],
               'view_endpoint' => [
                   'type' => 'string',
                   'analyzer' => 'no_analyzed'
               ],
               'view_keywords' => [
                   'type' => 'string',
                   'analyzer' => 'no_analyzed',
               ],
               'view_screen' => [
                   'type' => 'string',
                   'analyzer' => 'no_analyzed',
               ]
           ]
       ],*/
    public function createIndex($indexName,$indexTypeDataStruct)
    {

        if(!isset($indexTypeDataStruct)){
            $indexTypeDataStruct = new \stdClass();
        }
        $params=[
            'index' => $indexName,
            'body' => [
                'settings' => [
                    'number_of_shards' => 0,
                    'number_of_replicas' => 0
                ],
                'mappings' => [$indexTypeDataStruct]
            ],
        ];

        return $client->indices()->create($params);
    }

    public function insertDoc($indexName,$indexType,$indexID,$indexBody) //indexID 可以为ip值
    {
        $insert_params= [
            'index' => $indexName,
            'type' => $indexType,
            'id' => $indexID,
            'body' => $indexBody
        ];

        return $this->client->index($insert_params);
    }

    public function selectIndex($index,$type,$id)
    {
        $select_params= [
            'index' => $index,
            'type' => $type,
            'id' => $id
        ];

        return $this->client->get($select_params);
    }

    public function searchIndex($index, $type, $match_data)
    {
        $search_params= [
            'index' => $index,
            'type' => $type,
            'body' => [
                'query' => [
                    'match' => $match_data
                ]
            ]
        ];

        return $this->client->search($search_params);
    }

    public function deleteDoc($index, $type, $id)
    {
        $delete_params= [
            'index' => $index,
            'type' => $type,
            'id' => $id
        ];

        return $this->client->delete($delete_params);
    }

    public function deleteIndex()
    {
        $delete_params= [
            'index' => $delete_params
        ];

        return $this->client->indices->delete($delete_params);
    }

    public function upsertDoc($index, $type, $id, $upsertParams)
    {
        $upsertDoc_params= [
            'index' => $index,
            'type' => $type,
            'id' => $id,
            'body' => [
                'script' => 'ctx._source.counter += count',
                'params' => [
                    'count' => 1
                ]
            ]
        ];

        return $this->client->update($upsertDoc_params);
    }
}
